package View;

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import ViewListener.LoginViewActionListener;
import ViewListener.LoginViewItemListener;

public class LoginView extends MyView{

	private static JTextField txtUser;
	private static JTextField txtPass;
	public static String user = "";
	public static String password = "";
	private int focusNumber = 0;  
	private Component[] focusList;
	private LoginViewActionListener actionlistener;
	private LoginViewItemListener itemlistener;
	/**
	 * Create the application.
	 */
	public LoginView(String name) {
		super(name);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {		
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		frame = new JFrame();
		frame.setTitle("Login");
		frame.setBounds(100, 100, 300, 200);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		SpringLayout springLayout = new SpringLayout();
		frame.getContentPane().setLayout(springLayout);
		
		actionlistener = new LoginViewActionListener();
		itemlistener = new LoginViewItemListener();
		
		txtUser = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, txtUser, -32, SpringLayout.EAST, frame.getContentPane());
		frame.getContentPane().add(txtUser);
		txtUser.setColumns(10);
		
		JLabel lblUser = new JLabel("User");
		springLayout.putConstraint(SpringLayout.NORTH, txtUser, -3, SpringLayout.NORTH, lblUser);
		springLayout.putConstraint(SpringLayout.WEST, txtUser, 57, SpringLayout.EAST, lblUser);
		springLayout.putConstraint(SpringLayout.NORTH, lblUser, 23, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblUser, 27, SpringLayout.WEST, frame.getContentPane());
		frame.getContentPane().add(lblUser);
		
		JLabel lblNewLabel = new JLabel("Password");	
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 28, SpringLayout.SOUTH, lblUser);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 0, SpringLayout.WEST, lblUser);		
		frame.getContentPane().add(lblNewLabel);
		
		txtPass = new JPasswordField();
		springLayout.putConstraint(SpringLayout.NORTH, txtPass, -3, SpringLayout.NORTH, lblNewLabel);
		springLayout.putConstraint(SpringLayout.WEST, txtPass, 0, SpringLayout.WEST, txtUser);
		springLayout.putConstraint(SpringLayout.EAST, txtPass, -32, SpringLayout.EAST, frame.getContentPane());
		frame.getContentPane().add(txtPass);
		txtPass.setColumns(10);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(actionlistener);		
		springLayout.putConstraint(SpringLayout.NORTH, btnLogin, 25, SpringLayout.SOUTH, txtPass);
		springLayout.putConstraint(SpringLayout.WEST, btnLogin, 189, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, btnLogin, -32, SpringLayout.EAST, frame.getContentPane());
		frame.getContentPane().add(btnLogin);
		
		JCheckBox cbRemeber = new JCheckBox("Remember User & Password");
		cbRemeber.addItemListener(itemlistener);
		cbRemeber.setSelected(getSettingForCheckbox());
		springLayout.putConstraint(SpringLayout.NORTH, cbRemeber, 25, SpringLayout.SOUTH, txtPass);
		springLayout.putConstraint(SpringLayout.WEST, cbRemeber, 5, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, cbRemeber, -24, SpringLayout.WEST, btnLogin);
		frame.getContentPane().add(cbRemeber);

		setInformation();
		
		focusList = new Component[] { txtUser, txtPass, btnLogin, cbRemeber };
		
		frame.setFocusTraversalPolicy(new FocusTraversalPolicy() {
			
			@Override
			public Component getLastComponent(Container aContainer) {
				// TODO Auto-generated method stub
				return focusList[focusList.length - 1];
			}
			
			@Override
			public Component getFirstComponent(Container aContainer) {
				// TODO Auto-generated method stub
				return focusList[0];
			}
			
			@Override
			public Component getDefaultComponent(Container aContainer) {
				// TODO Auto-generated method stub
				return focusList[0];
			}
			
			@Override
			public Component getComponentBefore(Container aContainer,
					Component aComponent) {
				// TODO Auto-generated method stub
				  focusNumber = (focusList.length+focusNumber-1) % focusList.length;  
			      if(focusList[focusNumber].isEnabled() == false)  
			      {  
			        getComponentBefore(aContainer,focusList[focusNumber]);  
			      }  
			      return focusList[focusNumber];  
			}
			
			@Override
			public Component getComponentAfter(Container aContainer,
					Component aComponent) {
				// TODO Auto-generated method stub
				  focusNumber = (focusNumber+1) % focusList.length;  
			      if(focusList[focusNumber].isEnabled() == false)  
			      {  
			        getComponentAfter(aContainer,focusList[focusNumber]);  
			      }  
			      return focusList[focusNumber];  
			}
		});
	}
	
	static public void GetInformation(){
		user = txtUser.getText();
		password = txtPass.getText();
	}
	
	private void setInformation()
	{
		txtUser.setText(user);
		txtPass.setText(password);
	}
	
	private boolean getSettingForCheckbox()
	{
		boolean ret = false;
		File file = new File("config");
		
		try {
			if(!file.exists())
			{
				file.createNewFile();
				FileOutputStream fos = new FileOutputStream("config");
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.write(0);
				oos.flush();
				oos.close();
				fos.close();
			}
			else
			{
				FileInputStream fis = new FileInputStream("config");
				ObjectInputStream ois = new ObjectInputStream(fis);
				int cb = ois.read();
				if(cb == 1)
				{
					LoginView.user = ois.readUTF();
					LoginView.password = ois.readUTF();					
					ret = true;
				}				
				
				ois.close();
				fis.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return ret;
	}

}
